<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="/include/commonHeader::commonHeader('代码列表')"></head>
<body class="gray-bg layer-blue">
<div class="wrapper wrapper-content  animated fadeInRight full-container">
    <div class="row">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <div class="row">
                    <!--<div class="col-sm-3 col-xs-12">
                        <div id="codeTypeTree" class="ztree"></div>
                        &lt;!&ndash;<ul>
                            <li style="font-weight: bold" class="code_type active" data-codetype="">代码类型</li>
                            <li class="code_type" data-codetype="G2">岗位性质类型</li>
                            <li class="code_type" data-codetype="DUTY_TYPE">值班类型</li>
                            <li class="code_type" data-codetype="D1">不在位类型</li>
                            <li class="code_type" data-codetype="G1">加机组原因</li>
                        </ul>&ndash;&gt;
                    </div>-->
                    <div class="col-sm-12 col-xs-12">
                        <div class="btn-group table-tool" id="exampleToolbar" role="group">
                            <input id="query_cName" type="text" class="table-tool-btn  pull-left"
                                   placeholder="名称"/><input
                                id="query_cCode" type="text" class="table-tool-btn  pull-left"
                                placeholder="代码"/>
                            <button type="button" class="btn btn-outline btn-success btn-refresh" data-toggle="tooltip"
                                    title="查询" id="btn-search">
                                <i class="fa fa-search" aria-hidden="true"></i>
                            </button>
                            <shiro:hasPermission1 name="basicinfo:code:add">
                                <button id="btn_add" type="button" onclick="add()"
                                        class="btn btn-outline btn-success btn-refresh hidden" data-toggle="tooltip"
                                        title="添加">
                                    <i class="fa fa-plus" aria-hidden="true"></i>
                                </button>
                            </shiro:hasPermission1>
                            <button type="button" onclick="reload()" class="btn btn-outline btn-success "
                                    data-toggle="tooltip" title="刷新">
                                <i class="fa fa-refresh" aria-hidden="true"></i>
                            </button>
                        </div>
                        <table id="exampleTable" data-classes="table table-hover table-condensed">
                            <thead>
                            <tr>
                            </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var p_delete = 'hidden';
    var p_edit = 'hidden';
</script>
<shiro:hasPermission1 name="basicinfo:code:delete">
    <script type="text/javascript">
        p_delete = '';
    </script>
</shiro:hasPermission1>
<shiro:hasPermission1 name="basicinfo:code:edit">
    <script type="text/javascript">
        p_edit = '';
    </script>
</shiro:hasPermission1>

<script src="/plugins/bootstrapValidator/js/bootstrapValidator.min.js"></script>
<script src="/plugins/bootstrapValidator/js/language/zh_CN.js"></script>
<script type="text/javascript" src="/plugins/zTree/js/jquery.ztree.core.min.js"></script>

<!-- 自定义js -->
<script th:inline="javascript">
    var prefix = "/code";
    var parentCode = [[${parentCode}]];
    var moduleFlag = [[${moduleFlag}]];
    var setting = {
        data: {
            key: {
                title: "codetype"
            },
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onClick
        }
    };
    var zNodes = [
        {
            name: "代码类型", open: true,
            codetype: "",
            icon: "/plugins/zTree/css/zTreeStyle/img/common/typelist.png",
            children: [
                {name: "岗位性质类型", codetype: "G2", icon: "/plugins/zTree/css/zTreeStyle/img/common/type.png"},
                {name: "值班类型", codetype: "DUTY_TYPE", icon: "/plugins/zTree/css/zTreeStyle/img/common/type.png"},
                {name: "不在位类型", codetype: "D1", icon: "/plugins/zTree/css/zTreeStyle/img/common/type.png"},
                {name: "加机组原因", codetype: "G1", icon: "/plugins/zTree/css/zTreeStyle/img/common/type.png"}
            ]
        },

    ];
    $(function () {
        $.fn.zTree.init($("#codeTypeTree"), setting, zNodes);
        //初始化加载数据
        load();

        $("#btn-search").click(function () {
            $("#exampleTable").bootstrapTable('refresh');
        });

    });

    function onClick(event, treeId, treeNode, clickFlag) {
        parentCode = treeNode.codetype;
        if (parentCode != '') {
            $("#btn_add").removeClass("hidden");
        } else {
            $("#btn_add").addClass("hidden");
        }
        reload();
    }

    function load() {
        $('#exampleTable').bootstrapTable(
            {
                method: 'post',
                url: prefix + "/list.do",
                striped: true,
                dataType: "json",
                pagination: true,
                clickToSelect: true,
                pageSize: 10,
                pageNumber: 1,
                pageList: [10, 25, 50, 100],
                sidePagination: "server",
                paginationLoop: false,
                contentType: "application/x-www-form-urlencoded",
                queryParams: function (params) {
                    return {
                        parentCode: parentCode,
                        moduleFlag: moduleFlag,
                        cCode: $("#query_cCode").val(),
                        cName: $("#query_cName").val(),
                        limit: params.limit,
                        offset: params.offset
                    };
                },
                onDblClickRow: function (row, $element, field) {
                    view(row.parentCode, row.cCode,row.moduleFlag);
                },
                columns: [
                    {
                        field: 'id',
                        title: '序号',
                        formatter: orderFormatter,
                        valign: 'middle',
                        align: 'center',
                        width: '40'
                    },
                    {
                        field: 'parentCode',
                        title: '代码类型',
                        formatter: function (value, row, index) {
                            var result = '-';
                            if (value == 'G1') {
                                result = '加机组原因';
                            } else if (value == 'G2') {
                                result = '岗位性质类型';
                            } else if (value == 'S1') {
                                result = '值班类型';
                            } else if (value == 'D1') {
                                result = '不在位类型';
                            }

                            return result;

                        }
                    },
                    {
                        field: 'cCode',
                        title: '代码'
                    },
                    {
                        field: 'cName',
                        title: '名称'
                    },
                    {
                        field: 'idx',
                        title: '顺序'
                    },
                    {
                        field: "status",
                        title: "状态",
                        formatter: function (value, row, index) {
                            if (value == "1") {
                                return "有效";
                            } else if (value == "0") {
                                return "<label class='text-danger'>无效</label>";
                            }
                        }
                    },
                    {
                        field: 'updateByName',
                        title: '更新人'
                    },
                    {
                        field: 'updateTime',
                        title: '更新时间'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        visible: isShowOperation(),
                        formatter: function (value, row, index) {
                            return [
                                '<a class="' + p_edit + '" href="#" title="禁用/启动" onclick="toggleDisable(\'' + row.parentCode + '\',\'' + row.cCode + '\',\'' + row.status + '\',\'' + row.moduleFlag + '\')">',
                                '<i class="glyphicon glyphicon-info-sign"></i>',
                                '</a>&nbsp;&nbsp;',
                                '<a class="' + p_delete + '" href="#" title="删除" onclick="remove(\'' + row.parentCode + '\',\'' + row.cCode + '\',\'' + row.moduleFlag + '\')">',
                                '<i class="glyphicon glyphicon-remove text-danger"></i>',
                                '</a>&nbsp;&nbsp;'
                            ].join('');
                        }
                    }]
            })
    }
    //是否有权限显示 '操作' 列
    function isShowOperation() {
        return (p_edit != 'hidden' || p_delete != 'hidden');
    }

    //刷新
    function reload() {
        $("#query_cName").val("");
        $("#query_cCode").val("");
        $("#exampleTable").bootstrapTable('refresh');
    }

    //增加
    function add() {
        layer.open({
            type: 2,
            title: '新增',
            shadeClose: false, // 点击遮罩关闭层
            area: ['500px', '400px'],
            content: prefix + '/add.do?parentCode=' + parentCode + "&moduleFlag=" + moduleFlag
        });
    }


    //查看
    function view(parentCode, cCode,moduleFlag) {
        layer.open({
            type: 2,
            title: '查看',
            shadeClose: false, // 点击遮罩关闭层
            area: ['500px', '400px'],
            content: prefix + '/view.do?parentCode=' + parentCode + "&moduleFlag=" + moduleFlag + "&cCode=" + cCode
        });
    }

    //禁用/解除禁用
    function toggleDisable(parentCode, cCode, status,moduleFlag) {
        console.log(status);
        if (status == 1) {
            status = 0;
        } else {
            status = 1;
        }
        $.ajax({
            url: prefix + "/update.do",
            type: "post",
            data: {
                cCode: cCode,
                parentCode: parentCode,
                moduleFlag: moduleFlag,
                status: status
            },
            success: function (r) {
                if (r.code == 0) {
                    toastr.success(r.msg);
                    reload();
                } else {
                    toastr.warning(r.msg);
                }
            },
            error:function(){
                toastr.error('数据出错！');
            }
        });
    }

    //删除
    function remove(parentCode, cCode,moduleFlag) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/delete.do",
                type: "post",
                data: {
                    parentCode: parentCode,
                    cCode: cCode,
                    moduleFlag: moduleFlag
                },
                success: function (r) {
                    layer.close(index);
                    if (r.code == 0) {
                        toastr.success(r.msg);
                        reload();
                    } else {
                        toastr.warning(r.msg);
                    }
                },
                error:function(){
                    toastr.error('数据出错！');
                    layer.close(index);
                }
            });
        })
    }

</script>
</body>
</html>